﻿#include "interface.h"

SDL_Window* g_pWindow = NULL;         //窗口
SDL_Renderer* g_pRenderer = NULL;     //渲染器
Mix_Music* musicWin = NULL;              //音效
Mix_Music* musicFail = NULL;              //音效

extern Plaid g_plaidArr[50][50];   //单元格数组
extern Game g_game;		//游戏状态


Picture g_picBackGround;
Picture g_picBackFill;
//背景上
Picture g_picBackUpLeft;
Picture g_picBackUpMid;
Picture g_picBackUpRight;
//背景下
Picture g_picBackDownLU;
Picture g_picBackDownLD;
Picture g_picBackDownRU;
Picture g_picBackDownRD;
Picture g_picBackPU;
Picture g_picBackPD;
Picture g_picBackPR;
Picture g_picBackPL;

//笑脸
Picture g_picEnjoyWinUp;
Picture g_picEnjoyWinDown;
Picture g_picEnjoyFailUp;
Picture g_picEnjoyFailDown;
Picture g_picEnjoyOnUp;
Picture g_picEnjoyOnDown;

//成绩和时间
Picture g_picTime;
Picture g_picGrades;

//难度选择
Picture g_picHard;
Picture g_picEasy;
Picture g_picNormal;
Picture g_picCustom;

//自定义难度选择
Picture g_picThreeChoic;
Picture g_picSure;
Picture g_picButUp;
Picture g_picButDown;

//单元格
Picture g_picPlaidNormal;
Picture g_picPlaidEmpty;
Picture g_picPlaidNum_1;
Picture g_picPlaidNum_2;
Picture g_picPlaidNum_3;
Picture g_picPlaidNum_4;
Picture g_picPlaidNum_5;
Picture g_picPlaidNum_6;
Picture g_picPlaidNum_7;
Picture g_picPlaidNum_8;

//炸弹状态
Picture g_picPlaidBomb_0;
Picture g_picPlaidBomb_1;

//未知标记
Picture g_picPlaidTag;
Picture g_picPlaidUnknow;
Picture g_picPlaidQue;

//开始结束按钮
Picture g_picGameBegin;
Picture g_picGameEnd;

// 数字图片
Picture g_picNumber;   

//名称图片
Picture g_picName;


//=========================================================
// 名称：int loadWindows(int x,int y)
// 说明：传入单元格长宽，生成对应大小窗口
// 参数：int x,int y
//=========================================================
int loadWindows(int x,int y)
{

    if ((g_game.g_WindowX != max(x * 47 + 20 + 19, WINDOW_MIN_WIDTH))
        || g_game.g_WindowY != max(y * 47 + 30 + 21 + 80, WINDOW_MIN_HEIGHT))
    {
        g_game.g_WindowX = max(x * 47 + 20 + 19, WINDOW_MIN_WIDTH);
        g_game.g_WindowY = max(y * 47 + 30 + 21 + 80, WINDOW_MIN_HEIGHT);

        //释放资源
        freeResources();

        /*释放渲染器*/
        SDL_DestroyRenderer(g_pRenderer);

        /*释放窗口*/
        SDL_DestroyWindow(g_pWindow);

        /*创建窗口*/
        g_pWindow = SDL_CreateWindow("MineSweeper",
            SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
            g_game.g_WindowX, g_game.g_WindowY, SDL_WINDOW_SHOWN);
    
        if (NULL == g_pWindow)
        {
            printf("创建窗口失败：%s\n", SDL_GetError());
            return -2;
        }
        printf("窗口创建成功。\n");

        /*创建渲染器*/
        g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, SDL_RENDERER_ACCELERATED);
        if (NULL == g_pRenderer)
        {
            printf("创建渲染器失败：%s\n", SDL_GetError());
            return -3;
        }
        printf("渲染器创建成功。\n");

        //加载资源
        if (loadResources() != 0)
        {
            return -2;
        }
    }
    return 0;
}


//=========================================================
//名称：void freeResources(void)
//说明：释放程序加载的资源
//=========================================================
void freeResources(void)
{
    //释放背景
    SDL_DestroyTexture(g_picBackGround.pPic);
    SDL_DestroyTexture(g_picBackFill.pPic);
    SDL_DestroyTexture(g_picBackUpLeft.pPic);
    SDL_DestroyTexture(g_picBackUpMid.pPic);
    SDL_DestroyTexture(g_picBackUpRight.pPic);
    SDL_DestroyTexture(g_picBackDownLU.pPic);
    SDL_DestroyTexture(g_picBackDownLD.pPic);
    SDL_DestroyTexture(g_picBackDownRU.pPic);
    SDL_DestroyTexture(g_picBackDownRD.pPic);
    SDL_DestroyTexture(g_picBackPU.pPic);
    SDL_DestroyTexture(g_picBackPL.pPic);
    SDL_DestroyTexture(g_picBackPD.pPic);
    SDL_DestroyTexture(g_picBackPR.pPic);

    //释放笑脸
    SDL_DestroyTexture(g_picEnjoyWinUp.pPic);
    SDL_DestroyTexture(g_picEnjoyWinDown.pPic);
    SDL_DestroyTexture(g_picEnjoyFailUp.pPic);
    SDL_DestroyTexture(g_picEnjoyFailDown.pPic);
    SDL_DestroyTexture(g_picEnjoyOnUp.pPic);
    SDL_DestroyTexture(g_picEnjoyOnDown.pPic);

    //释放成绩和时间
    SDL_DestroyTexture(g_picTime.pPic);
    SDL_DestroyTexture(g_picGrades.pPic);

    //释放难度选择
    SDL_DestroyTexture(g_picHard.pPic);
    SDL_DestroyTexture(g_picEasy.pPic);
    SDL_DestroyTexture(g_picNormal.pPic);
    SDL_DestroyTexture(g_picCustom.pPic);

    //释放自定义难度选择
    SDL_DestroyTexture(g_picThreeChoic.pPic);
    SDL_DestroyTexture(g_picSure.pPic);
    SDL_DestroyTexture(g_picButUp.pPic);
    SDL_DestroyTexture(g_picButDown.pPic);

    //释放单元格
    SDL_DestroyTexture(g_picPlaidNormal.pPic);
    SDL_DestroyTexture(g_picPlaidNum_1.pPic);
    SDL_DestroyTexture(g_picPlaidNum_2.pPic);
    SDL_DestroyTexture(g_picPlaidNum_3.pPic);
    SDL_DestroyTexture(g_picPlaidNum_4.pPic);
    SDL_DestroyTexture(g_picPlaidNum_5.pPic);
    SDL_DestroyTexture(g_picPlaidNum_6.pPic);
    SDL_DestroyTexture(g_picPlaidNum_7.pPic);
    SDL_DestroyTexture(g_picPlaidNum_8.pPic);
    SDL_DestroyTexture(g_picPlaidBomb_0.pPic);
    SDL_DestroyTexture(g_picPlaidBomb_1.pPic);
    SDL_DestroyTexture(g_picPlaidTag.pPic);
    SDL_DestroyTexture(g_picPlaidUnknow.pPic);
    SDL_DestroyTexture(g_picPlaidQue.pPic);
    SDL_DestroyTexture(g_picPlaidEmpty.pPic);

    //释放按钮
    SDL_DestroyTexture(g_picGameBegin.pPic);
    SDL_DestroyTexture(g_picGameEnd.pPic);

    //释放音乐
    if(SHOW_WIN_MUSIC) Mix_FreeMusic(musicWin);
    if(SHOW_FAIL_MUSIC) Mix_FreeMusic(musicFail);

    //释放名字
    if(SHOW_NAME) SDL_DestroyTexture(g_picName.pPic);

    //释放数字图片
    SDL_DestroyTexture(g_picNumber.pPic);

}

//=========================================================
// 名称：void paint(void)
// 说明：绘制窗口
//=========================================================
void paint(void)
{
    //绘制背景图片
    paintBackground();

    //绘制按钮
    paintButton();
    
    //绘制笑脸
    paintEnjoy();

    //绘制单元格
    paintPlain();

    //绘制自定义难度的数字
    paintThreeNumber();

    //绘制分数
    paintScore();

    //绘制时间
    paintTime();
    
    // 更新渲染器图像
    SDL_RenderPresent(g_pRenderer);
}
//=========================================================
// 名称：void paintThreeNumber(void)
// 说明：绘制自定义难度数字
//=========================================================
void paintThreeNumber(void)
{
    if (g_game.gamemode == ModeU)
    {
        //横
        int nScoreBit = 0;          //多少位
        int nCurScore = g_game.g_unPlaidXNum;   //获得数字
        int nCurBitNumber = 0;      //偏移量
        int i, j;
        while (nCurScore > 0)
        {
            nCurScore /= 10;
            nScoreBit++;
        }

        if (g_game.g_unPlaidXNum == 0)
        {
            nScoreBit = 1;
        }

        for (i = 0; i < nScoreBit; i++)
        {
            g_picNumber.dstRect.x = g_game.g_WindowX - BUT_X_X - 32 + i * NUMBER_DST_W;
            g_picNumber.dstRect.y = BUT_UP_Y + 3;

            nCurScore = g_game.g_unPlaidXNum;
            for (j = 1; j < (nScoreBit - i); j++)
            {
                nCurScore /= 10;
            }
            nCurBitNumber = nCurScore % 10;
            g_picNumber.srcRect.x = nCurBitNumber * NUMBER_SRC_W;
            g_picNumber.srcRect.w = NUMBER_SRC_W;
            paintPic(&g_picNumber);
        }

        //竖
        nScoreBit = 0;          //多少位
        nCurScore = g_game.g_unPlaidYNum;   //获得数字
        nCurBitNumber = 0;      //偏移量
        while (nCurScore > 0)
        {
            nCurScore /= 10;
            nScoreBit++;
        }

        if (g_game.g_unPlaidYNum == 0)
        {
            nScoreBit = 1;
        }

        for (i = 0; i < nScoreBit; i++)
        {
            g_picNumber.dstRect.x = g_game.g_WindowX - BUT_Y_X - 32 + i * NUMBER_DST_W;
            g_picNumber.dstRect.y = BUT_UP_Y + 3;

            nCurScore = g_game.g_unPlaidYNum;
            for (j = 1; j < (nScoreBit - i); j++)
            {
                nCurScore /= 10;
            }
            nCurBitNumber = nCurScore % 10;
            g_picNumber.srcRect.x = nCurBitNumber * NUMBER_SRC_W;
            g_picNumber.srcRect.w = NUMBER_SRC_W;
            paintPic(&g_picNumber);
        }

        //雷
        nScoreBit = 0;          //多少位
        nCurScore = g_game.g_unbombNum;   //获得数字
        nCurBitNumber = 0;      //偏移量
        while (nCurScore > 0)
        {
            nCurScore /= 10;
            nScoreBit++;
        }

        if (g_game.g_unbombNum == 0)
        {
            nScoreBit = 1;
        }

        for (i = 0; i < nScoreBit; i++)
        {
            g_picNumber.dstRect.x = g_game.g_WindowX - BUT_N_X - 32 + i * NUMBER_DST_W;
            g_picNumber.dstRect.y = BUT_UP_Y + 3;

            nCurScore = g_game.g_unbombNum;
            for (j = 1; j < (nScoreBit - i); j++)
            {
                nCurScore /= 10;
            }
            nCurBitNumber = nCurScore % 10;
            g_picNumber.srcRect.x = nCurBitNumber * NUMBER_SRC_W;
            g_picNumber.srcRect.w = NUMBER_SRC_W;
            paintPic(&g_picNumber);
        }
    }
}

//=========================================================
// 名称：void paintTime(void)
// 说明：绘制时间
//=========================================================
void paintTime(void)
{
    int nScoreBit = 0;          //多少位
    int nCurScore = g_game.g_time;   //获得数字
    int nCurBitNumber = 0;      //偏移量
    int i, j;
    while (nCurScore > 0)
    {
        nCurScore /= 10;
        nScoreBit++;
    }

    if (g_game.g_time == 0)
    {
        nScoreBit = 1;
    }

    for (i = 0; i < nScoreBit; i++)
    {
        g_picNumber.dstRect.x = g_game.g_WindowX - 160 + i * NUMBER_DST_W;
        g_picNumber.dstRect.y = GRADES_DST_Y;

        nCurScore = g_game.g_time;
        for (j = 1; j < (nScoreBit - i); j++)
        {
            nCurScore /= 10;
        }
        nCurBitNumber = nCurScore % 10;
        g_picNumber.srcRect.x = nCurBitNumber * NUMBER_SRC_W;
        g_picNumber.srcRect.w = NUMBER_SRC_W;
        paintPic(&g_picNumber);//绘制时间图片
    }
}

//=========================================================
// 名称：void paintEnjoy(void)
// 说明：根据游戏进行状态绘制表情
//=========================================================
void paintEnjoy(void)
{
    switch (g_game.g_enjoy)
    {
    case EnjoyWinUp:
        paintPic(&g_picEnjoyWinUp);
        break;
    case EnjoyWinDown:
        paintPic(&g_picEnjoyWinDown);
        break;
    case EnjoyFailUp:
        paintPic(&g_picEnjoyFailUp);
        break;
    case EnjoyFailDown:
        paintPic(&g_picEnjoyFailDown);
        break;
    case EnjoyNormalUp:
        paintPic(&g_picEnjoyOnUp);
        break;
    case EnjoyNormalDown:
        paintPic(&g_picEnjoyOnDown);
        break;
    default:
        break;
    }
}

//=========================================================
// 名称：void paintButton(void)
// 说明：绘制按钮
//=========================================================
void paintButton()
{
    Picture* pPic = NULL;

    //开始按钮
    pPic = &g_picGameBegin;
    pPic->dstRect.x = PATH_GAME_BEGIN_X;
    pPic->dstRect.y = PATH_GAME_Y;
    paintPic(pPic);

    //结束按钮
    pPic = &g_picGameEnd;
    pPic->dstRect.x = PATH_GAME_END_X;
    pPic->dstRect.y = PATH_GAME_Y;
    paintPic(pPic);

    //难度切换按钮
    pPic = &g_picButUp;//上
    pPic->dstRect.y = BUT_UP_Y;
    pPic->dstRect.x = BUT_X;
    paintPic(pPic);
    pPic = &g_picButDown;//下
    pPic->dstRect.y = BUT_DOWN_Y;
    pPic->dstRect.x = BUT_X;
    paintPic(pPic);

    switch (g_game.gamemode)
    {
    case ModeE:
        //基础难度
        pPic = &g_picEasy;
        pPic->dstRect.x = PATH_BUT_EASY_X;
        pPic->dstRect.y = PATH_BUT_EASY_Y;
        paintPic(pPic);
        break;
    case ModeN:
        //普通难度
        pPic = &g_picNormal;
        pPic->dstRect.x = PATH_BUT_NORMAL_X;
        pPic->dstRect.y = PATH_BUT_NORMAL_Y;
        paintPic(pPic);
        break;
    case ModeH:
        //困难难度
        pPic = &g_picHard;
        pPic->dstRect.x = PATH_BUT_HARD_X;
        pPic->dstRect.y = PATH_BUT_HARD_Y;
        paintPic(pPic);
       break;
    case ModeU:
        //自定义难度
        pPic = &g_picCustom;
        pPic->dstRect.x = PATH_BUT_CUSTOMIZE_X;
        pPic->dstRect.y = PATH_BUT_CUSTOMIZE_Y;
        paintPic(pPic);

        //确定按钮
        pPic = &g_picSure;
        pPic->dstRect.x = g_game.g_WindowX - SURE_X - SURE_W;
        pPic->dstRect.y = SURE_Y;
        paintPic(pPic);

        //三个上建
        pPic = &g_picButUp;
        pPic->dstRect.y = BUT_UP_Y;
        pPic->dstRect.x = g_game.g_WindowX - BUT_N_X;
        paintPic(pPic);
        pPic->dstRect.x = g_game.g_WindowX - BUT_Y_X;
        paintPic(pPic);
        pPic->dstRect.x = g_game.g_WindowX - BUT_X_X;
        paintPic(pPic);

        //三个下建
        pPic = &g_picButDown;
        pPic->dstRect.y = BUT_DOWN_Y;
        pPic->dstRect.x = g_game.g_WindowX - BUT_N_X;
        paintPic(pPic);
        pPic->dstRect.x = g_game.g_WindowX - BUT_Y_X;
        paintPic(pPic);
        pPic->dstRect.x = g_game.g_WindowX - BUT_X_X;
        paintPic(pPic);
        break;
    default:
        break;
    }
}

//=========================================================
// 名称：void paintBackground(void)
// 说明：绘制背景
//=========================================================
void paintBackground(void)
{
    Picture* pPic = NULL;

    //上面部分
    pPic = &g_picBackUpLeft;//上左
    pPic->dstRect.x = 0;
    pPic->dstRect.y = 0;
    paintPic(pPic);

    pPic = &g_picBackUpRight;//上右
    pPic->dstRect.x = g_game.g_WindowX - pPic->dstRect.w;
    pPic->dstRect.y = 0;
    paintPic(pPic);

    //判断是否要延长中间部分
    if (g_game.g_WindowX >
        g_picBackUpLeft.srcRect.w + g_picBackUpRight.srcRect.w)
    {
        pPic = &g_picBackUpMid;
        for (int i = 0; i < g_game.g_PlaidXNum - BACK_UP_TEST; i++)
        {
            pPic->dstRect.x = g_picBackUpLeft.dstRect.w + i * pPic->dstRect.w;
            paintPic(pPic);
        }
    }

    //绘制下面
    pPic = &g_picBackDownLU;//左上
    pPic->dstRect.x = 0;
    pPic->dstRect.y = g_picBackUpLeft.srcRect.h;
    paintPic(pPic);

    pPic = &g_picBackDownLD;//左下
    pPic->dstRect.x = 0;
    pPic->dstRect.y = g_picBackUpLeft.srcRect.h + g_picBackDownLU.srcRect.h + PLAID_H * g_game.g_PlaidYNum;
    paintPic(pPic);

    pPic = &g_picBackDownRU;//右上
    pPic->dstRect.x = g_picBackDownLU.srcRect.w + g_game.g_PlaidXNum * PLAID_W;
    pPic->dstRect.y = g_picBackUpLeft.srcRect.h;
    paintPic(pPic);

    pPic = &g_picBackDownRD;//右下
    pPic->dstRect.x = g_picBackDownLU.srcRect.w + g_game.g_PlaidXNum * PLAID_W;
    pPic->dstRect.y = g_picBackUpLeft.srcRect.h + g_picBackDownLU.srcRect.h + PLAID_H * g_game.g_PlaidYNum;
    paintPic(pPic);

    //单元格部分
    pPic = &g_picBackPU;//上
    for (int i = 0; i < g_game.g_PlaidXNum; i++)
    {
        pPic->dstRect.x = g_picBackDownLU.srcRect.w + i * PLAID_W;
        pPic->dstRect.y = g_picBackUpLeft.srcRect.h;
        paintPic(pPic);
    }

    pPic = &g_picBackPD;//下
    for (int i = 0; i < g_game.g_PlaidXNum; i++)
    {
        pPic->dstRect.x = g_picBackDownLU.srcRect.w + i * PLAID_W;
        pPic->dstRect.y = g_picBackUpLeft.srcRect.h + g_picBackDownLU.srcRect.h + PLAID_H * g_game.g_PlaidYNum;
        paintPic(pPic);
    }

    pPic = &g_picBackPL;//左
    for (int i = 0; i < g_game.g_PlaidYNum; i++)
    {
        pPic->dstRect.x = 0;
        pPic->dstRect.y = g_picBackUpLeft.srcRect.h + g_picBackDownLU.srcRect.h + i * PLAID_H;
        paintPic(pPic);
    }

    pPic = &g_picBackPR;//右
    for (int i = 0; i < g_game.g_PlaidYNum; i++)
    {
        pPic->dstRect.x = g_picBackDownLU.srcRect.w + g_game.g_PlaidXNum * PLAID_W;
        pPic->dstRect.y = g_picBackUpLeft.srcRect.h + g_picBackDownLU.srcRect.h + i * PLAID_H;
        paintPic(pPic);
    }

    //背景填充，不产生空位
    pPic = &g_picBackFill;
    //下侧填充
    pPic->dstRect.x = 0;
    pPic->dstRect.y = g_picBackUpLeft.srcRect.h 
        + g_picBackDownLU.srcRect.h 
        + PLAID_H * g_game.g_PlaidYNum
        + g_picBackDownLD.srcRect.h;
    paintPic(pPic);

    //右侧填充
    pPic->dstRect.y = g_picBackUpLeft.srcRect.h;
    pPic->dstRect.x = g_picBackDownLU.srcRect.w 
        + g_game.g_PlaidXNum * PLAID_W
        + g_picBackDownRD.srcRect.w;
    paintPic(pPic);

    //固定文字提示
    pPic = &g_picTime;//时间提示
    pPic->dstRect.x = g_game.g_WindowX - TIME_X;
    pPic->dstRect.y = TIME_Y;
    paintPic(pPic);
    pPic = &g_picGrades;//成绩提示
    pPic->dstRect.x = g_game.g_WindowX - GRADES_X;
    pPic->dstRect.y = GRADES_Y;
    paintPic(pPic);

    //设置自定义难度三个选项提示
    if (g_game.gamemode == ModeU)
    {
        pPic = &g_picThreeChoic;
        pPic->dstRect.x = g_game.g_WindowX - THREE_CHOIC_X;
        pPic->dstRect.y = THREE_CHOIC_Y;
        paintPic(pPic);
    }

    if (SHOW_NAME)
    {
        pPic = &g_picName;//姓名
        pPic->dstRect.x = 0;
        pPic->dstRect.y = g_picBackUpLeft.srcRect.h + g_picBackDownLU.srcRect.h + PLAID_H * g_game.g_PlaidYNum + g_picBackDownLD.srcRect.h / 2;
        paintPic(pPic);
    }
}

//=========================================================
// 名称：void paintPlain(void)
// 说明：绘制单元格
//=========================================================
void paintPlain(void)
{
    int i,j;
    Picture* pPict = NULL;
    // 遍历所有的单元格，根据类型确定绘制图片
    for (i = 0; i < g_game.g_PlaidYNum; i++)
    {
        for (j = 0; j < g_game.g_PlaidXNum; j++)
        {
            if (g_plaidArr[i][j].state == StateUnknow) // 未知
            {
                if (g_plaidArr[i][j].type_un == TypeNormal) pPict = &g_picPlaidNormal;
                if (g_plaidArr[i][j].type_un == TypeTag) pPict = &g_picPlaidTag;
                if (g_plaidArr[i][j].type_un == TypeQue) pPict = &g_picPlaidUnknow;
            }
            else if(g_plaidArr[i][j].state == StateKnow)
            {
                if (g_plaidArr[i][j].type == TypeEmpty) // 无类型
                {
                    pPict = &g_picPlaidEmpty;
                }
                else if (g_plaidArr[i][j].type == TypeNum)
                {
                    pPict = chosPic(i, j);
                }
                else if (g_plaidArr[i][j].type == TypeBomb)
                {
                    pPict = &g_picPlaidBomb_0;
                }
                else if (g_plaidArr[i][j].type == TypeBombed)
                {
                    pPict = &g_picPlaidBomb_1;
                }
            }
            if (pPict != NULL)
            {
                pPict->dstRect = g_plaidArr[i][j].initRect;
                paintPic(pPict);
            }
            else
            {
                printf("pPict is null\n");
            }
        }
    }
}

//=========================================================
//名称：void paintScore(void)
// 说明：打印分数
//=========================================================
void paintScore(void)
{
    int nScoreBit = 0;          //分数多少位
    int nCurScore = g_game.g_grades;   //获得分数
    int nCurBitNumber = 0;      //偏移量
    int i, j;
    while (nCurScore > 0)
    {
        nCurScore /= 10;
        nScoreBit++;
    }

    if (g_game.g_grades == 0)
    {
        nScoreBit = 1;
    }

    for (i = 0; i < nScoreBit; i++)
    {
        g_picNumber.dstRect.x = g_game.g_WindowX - 60 + i * NUMBER_DST_W;
        g_picNumber.dstRect.y = GRADES_DST_Y;

        nCurScore = g_game.g_grades;
        for (j = 1; j < (nScoreBit - i); j++)
        {
            nCurScore /= 10;
        }
        nCurBitNumber = nCurScore % 10;
        g_picNumber.srcRect.x = nCurBitNumber * NUMBER_SRC_W;
        g_picNumber.srcRect.w = NUMBER_SRC_W;
        paintPic(&g_picNumber);//绘制分数图片
    }
}

//=========================================================
//名称：chosPic( int i, int j)
// 说明：跟据所在位置周围的雷选择输出的图片
// 参数：传入位置,传入地址
// 返回：选择图片的地址
// ========================================================
Picture* chosPic(int i, int j)
{
    int numb, x, y, a;

    numb = 0;
  
    int dir[8][2] = { 0,1, 1,1, 1,0, 1,-1, 0,-1, -1,-1, -1,0, -1,1 };
    //开始在地雷周围变成数字类型
    for (a = 0; a < 8; a++)
    {
        y = i + dir[a][0];
        x = j + dir[a][1];
        if (y >= 0 && y < g_game.g_PlaidYNum && x >= 0 && x < g_game.g_PlaidXNum
              && (g_plaidArr[y][x].type == TypeBomb || g_plaidArr[y][x].type == TypeBombed))
        { 
            numb++;
        }
    }
            

    switch (numb)
    {
    case 1:
    {
        return &g_picPlaidNum_1;
        break;
    }
    case 2:
    {
        return  &g_picPlaidNum_2;
        break;
    }
    case 3:
    {
        return  &g_picPlaidNum_3;
        break;
    }
    case 4:
    {
        return  &g_picPlaidNum_4;
        break;
    }
    case 5:
    {
        return  &g_picPlaidNum_5;
        break;
    }
    case 6:
    {
        return  &g_picPlaidNum_6;
        break;
    }
    case 7:
    {
        return  &g_picPlaidNum_7;
        break;
    }
    case 8:
    {
        return  &g_picPlaidNum_8;
        break;
    }
    default:
        break;
    }
    return  NULL;
}

//=========================================================
// 名称 :void playMusic(void)
// 说明：根据游戏状态播放对应音乐
//=========================================================
void playMusic(void)
{
    switch (g_game.gamestate)
    {
    case Win:
        if(SHOW_WIN_MUSIC) Mix_PlayMusic(musicWin, 1);
        break;
    case Fail:
        if(SHOW_FAIL_MUSIC) Mix_PlayMusic(musicFail, 1);
        break;
    default:
        break;
    }
}

//=========================================================
// 名称：int loadResources(void)
// 说明：加载程序所需的资源
// 返回：0表示加载成功，非0表示加载失败
//=========================================================
int loadResources(void)
{
    //加载音乐
    if (SHOW_WIN_MUSIC)//胜利音乐
    {
        musicWin = Mix_LoadMUS(PATH_MUSIC_WIN);
        if (musicWin == NULL)
        {
            printf("music load fail:%s\n", SDL_GetError());
            return -1;
        }
    }
    if (SHOW_FAIL_MUSIC)//失败音乐
    {
        musicFail = Mix_LoadMUS(PATH_MUSIC_FAIL);
        if (musicFail == NULL)
        {
            printf("music load fail:%s\n", SDL_GetError());
            return -1;
        }
    }

    //加载姓名图片
    if (SHOW_NAME)
    {
        g_picName = loadPic(PATH_NAME);
        if (g_picName.pPic == NULL)
        {
            return -1;
        }
        setPicSrcRect(&g_picName, 0, 0, 0, 0);
        setPicDstRect(&g_picName, 0, 0, NAME_W, NAME_H);
    }
    
    //加载背景图片
    g_picBackUpLeft = loadPic(PATH_BACK_UP_LEFT);//上左
    if (g_picBackUpLeft.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picBackUpLeft, 0, 0, 0, 0);
    setPicDstRect(&g_picBackUpLeft, 0, 0, BACE_UP_LEFT_W, BACE_UP_LEFT_H);
    g_picBackUpRight = loadPic(PATH_BACK_UP_RIGHT);//上右
    if (g_picBackUpRight.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picBackUpRight, 0, 0, 0, 0);
    setPicDstRect(&g_picBackUpRight, 0, 0, BACE_UP_RIGHT_W, BACE_UP_RIGHT_H);
    g_picBackUpMid = loadPic(PATH_BACK_UP_MI);//上中
    if (g_picBackUpMid.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picBackUpMid, 0, 0, 0, 0);
    setPicDstRect(&g_picBackUpMid, 0, 0, BACE_UP_MI_W, BACE_UP_MI_H);
    g_picBackDownLU = loadPic(PATH_BACK_DUP_LEFT);//左上
    if (g_picBackDownLU.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picBackDownLU, 0, 0, 0, 0);
    setPicDstRect(&g_picBackDownLU, 0, 0, BACE_DOWN_LU_W, BACE_DOWN_LU_H);
    g_picBackDownRU = loadPic(PATH_BACK_DUP_RIGHT);//右上
    if (g_picBackDownRU.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picBackDownRU, 0, 0, 0, 0);
    setPicDstRect(&g_picBackDownRU, 0, 0, BACE_DOWN_RU_W, BACE_DOWN_RU_H);
    g_picBackDownRD = loadPic(PATH_BACK_DDOWN_RIGHT);//右下
    if (g_picBackDownRD.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picBackDownRD, 0, 0, 0, 0);
    setPicDstRect(&g_picBackDownRD, 0, 0, BACE_DOWN_RD_W, BACE_DOWN_RD_H);
    g_picBackDownLD = loadPic(PATH_BACK_DDOWN_LEFT);//左下
    if (g_picBackDownLD.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picBackDownLD, 0, 0, 0, 0);
    setPicDstRect(&g_picBackDownLD, 0, 0, BACE_DOWN_LD_W, BACE_DOWN_LD_H);

    g_picBackPU= loadPic(PATH_BACK_PL_UP);
    if (g_picBackPU.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picBackPU, 0, 0, 0, 0);
    setPicDstRect(&g_picBackPU, 0, 0, BACE_PL_UP_W, BACE_PL_UP_H);
    g_picBackPD= loadPic(PATH_BACK_PL_DOWN);
    if (g_picBackPD.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picBackPD, 0, 0, 0, 0);
    setPicDstRect(&g_picBackPD, 0, 0, BACE_PL_DOWN_W, BACE_PL_DOWN_H);
    g_picBackPL = loadPic(PATH_BACK_PL_LEFT);
    if (g_picBackPL.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picBackPL, 0, 0, 0, 0);
    setPicDstRect(&g_picBackPL, 0, 0, BACE_PL_LEFT_W, BACE_PL_LEFT_H);
    g_picBackPR = loadPic(PATH_BACK_PL_RIGHT);
    if (g_picBackPR.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picBackPR, 0, 0, 0, 0);
    setPicDstRect(&g_picBackPR, 0, 0, BACE_PL_RIGHT_W, BACE_PL_RIGHT_H);

    //加载背景填充图片
    g_picBackFill = loadPic(PATH_BACK_FILL);
    if (g_picBackFill.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picBackFill, 0, 0, 0, 0);
    setPicDstRect(&g_picBackFill, 0, 0, BACE_FILL_W, BACE_FILL_H);

    //加载笑脸
    g_picEnjoyWinUp = loadPic(PATH_ENJOY_WIN_UP);
    if (g_picEnjoyWinUp.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picEnjoyWinUp, 0, 0, 0, 0);
    setPicDstRect(&g_picEnjoyWinUp, (g_game.g_WindowX - ENJOY_W) / 2, 25, ENJOY_W, ENJOY_H);
    g_picEnjoyWinDown = loadPic(PATH_ENJOY_WIN_DOWN);
    if (g_picEnjoyWinDown.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picEnjoyWinDown, 0, 0, 0, 0);
    setPicDstRect(&g_picEnjoyWinDown, (g_game.g_WindowX - ENJOY_W) / 2, 25, ENJOY_W, ENJOY_H);
    g_picEnjoyFailUp = loadPic(PATH_ENJOY_FAIL_UP);
    if (g_picEnjoyFailUp.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picEnjoyFailUp, 0, 0, 0, 0);
    setPicDstRect(&g_picEnjoyFailUp, (g_game.g_WindowX - ENJOY_W) / 2, 25, ENJOY_W, ENJOY_H);
    g_picEnjoyFailDown = loadPic(PATH_ENJOY_FAIL_DOWN);
    if (g_picEnjoyFailDown.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picEnjoyFailDown, 0, 0, 0, 0);
    setPicDstRect(&g_picEnjoyFailDown, (g_game.g_WindowX - ENJOY_W) / 2, 25, ENJOY_W, ENJOY_H);
    g_picEnjoyOnUp = loadPic(PATH_ENJOY_NORMAL_UP);
    if (g_picEnjoyOnUp.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picEnjoyOnUp, 0, 0, 0, 0);
    setPicDstRect(&g_picEnjoyOnUp, (g_game.g_WindowX - ENJOY_W) / 2, 25, ENJOY_W, ENJOY_H);
    g_picEnjoyOnDown = loadPic(PATH_ENJOY_NORMAL_DOWN);
    if (g_picEnjoyOnDown.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picEnjoyOnDown, 0, 0, 0, 0);
    setPicDstRect(&g_picEnjoyOnDown, (g_game.g_WindowX-ENJOY_W)/2, 25, ENJOY_W, ENJOY_H);

    //加载成绩时间
    g_picTime = loadPic(PATH_TIME);
    if (g_picTime.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picTime, 0, 0, 0, 0);
    setPicDstRect(&g_picTime, 0, 0, TIME_W, TIME_H);
    g_picGrades = loadPic(PATH_GRADES);
    if (g_picGrades.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picGrades, 0, 0, 0, 0);
    setPicDstRect(&g_picGrades, 0, 0, GRADES_W, GRADES_H);

    //加载难度选择
    g_picEasy = loadPic(PATH_BUT_EASY);
    if (g_picEasy.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picEasy, 0, 0, 0, 0);
    setPicDstRect(&g_picEasy, 0, 0, PATH_BUT_EASY_W, PATH_BUT_EASY_H);
    g_picNormal = loadPic(PATH_BUT_NORMAL);
    if (g_picNormal.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picNormal, 0, 0, 0, 0);
    setPicDstRect(&g_picNormal, 0, 0, PATH_BUT_NORMAL_W, PATH_BUT_NORMAL_H);
    g_picHard = loadPic(PATH_BUT_HARD);
    if (g_picHard.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picHard, 0, 0, 0, 0);
    setPicDstRect(&g_picHard, 0, 0, PATH_BUT_HARD_W, PATH_BUT_HARD_H);
    g_picCustom = loadPic(PATH_BUT_CUSTOMIZE);
    if (g_picCustom.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picCustom, 0, 0, 0, 0);
    setPicDstRect(&g_picCustom, 0, 0, PATH_BUT_CUSTOMIZE_W, PATH_BUT_CUSTOMIZE_H);

    //加载自定义难度
    g_picThreeChoic = loadPic(PATH_THREE_CHOIC);
    if (g_picThreeChoic.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picThreeChoic, 0, 0, 0, 0);
    setPicDstRect(&g_picThreeChoic, 0, 0, THREE_CHOIC_W, THREE_CHOIC_H);
    g_picSure = loadPic(PATH_SURE);
    if (g_picSure.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picSure, 0, 0, 0, 0);
    setPicDstRect(&g_picSure, 0, 0, SURE_W, SURE_H);
    g_picButUp = loadPic(PATH_BUT_UP);
    if (g_picButUp.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picButUp, 0, 0, 0, 0);
    setPicDstRect(&g_picButUp, 0, 0, BUT_W, BUT_H);
    g_picButDown = loadPic(PATH_BUT_DOWN);
    if (g_picButDown.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picButDown, 0, 0, 0, 0);
    setPicDstRect(&g_picButDown, 0, 0, BUT_W, BUT_H);

    //加载开始游戏的图片
    g_picGameBegin = loadPic(PATH_GAME_BEGIN);
    if (g_picGameBegin.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picGameBegin, 0, 0, 0, 0);
    setPicDstRect(&g_picGameBegin, 0, 0, PATH_GAME_W, PATH_GAME_H);

    //加载游戏结束的图片
    g_picGameEnd = loadPic(PATH_GAME_END);
    if (g_picGameEnd.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picGameEnd, 0, 0, 0, 0);
    setPicDstRect(&g_picGameEnd, 0, 0, PATH_GAME_W, PATH_GAME_H);

    //加载旗子的图片
    g_picPlaidTag = loadPic(PATH_PLAID_TAG);
    if (g_picPlaidTag.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picPlaidTag, 0, 0, 0, 0);

    //加载问号的图片
    g_picPlaidUnknow = loadPic(PATH_PLAID_UNKNOW);
    if (g_picPlaidUnknow.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picPlaidUnknow, 0, 0, 0, 0);

    //加载没炸的炸弹
    g_picPlaidBomb_0 = loadPic(PATH_PLAID_BOMB_1);
    if (g_picPlaidBomb_0.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picPlaidBomb_0, 0, 0, 0, 0);

    //加载已经炸了的图片
    g_picPlaidBomb_1 = loadPic(PATH_PLAID_BOMB_2);
    if (g_picPlaidBomb_1.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picPlaidBomb_1, 0, 0, 0, 0);

    //加载未点开的图片
    g_picPlaidNormal = loadPic(PATH_PLAID_NORMAL);
    if (g_picPlaidNormal.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picPlaidNormal, 0, 0, 0, 0);

    //加载空的图片
    g_picPlaidEmpty = loadPic(PATH_PLAID_EMPTY);
    if (g_picPlaidEmpty.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picPlaidEmpty, 0, 0, 0, 0);

    //加载数字图片1
    g_picPlaidNum_1 = loadPic(PATH_PLAID_1);
    if (g_picPlaidNum_1.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picPlaidNum_1, 0, 0, 0, 0);

    //加载数字图片2
    g_picPlaidNum_2 = loadPic(PATH_PLAID_2);
    if (g_picPlaidNum_2.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picPlaidNum_2, 0, 0, 0, 0);

    //加载数字图片3
    g_picPlaidNum_3 = loadPic(PATH_PLAID_3);
    if (g_picPlaidNum_3.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picPlaidNum_3, 0, 0, 0, 0);

    //加载数字图片4
    g_picPlaidNum_4 = loadPic(PATH_PLAID_4);
    if (g_picPlaidNum_4.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picPlaidNum_4, 0, 0, 0, 0);
    
    //加载数字图片5
    g_picPlaidNum_5 = loadPic(PATH_PLAID_5);
    if (g_picPlaidNum_5.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picPlaidNum_5, 0, 0, 0, 0);

    //加载数字图片6
    g_picPlaidNum_6 = loadPic(PATH_PLAID_6);
    if (g_picPlaidNum_6.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picPlaidNum_6, 0, 0, 0, 0);

    //加载数字图片7
    g_picPlaidNum_7 = loadPic(PATH_PLAID_7);
    if (g_picPlaidNum_7.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picPlaidNum_7, 0, 0, 0, 0);

    //加载数字图片8
    g_picPlaidNum_8 = loadPic(PATH_PLAID_8);
    if (g_picPlaidNum_8.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picPlaidNum_8, 0, 0, 0, 0);

    //加载分数图片
    g_picNumber = loadPic(PATH_NUMBER);
    if (g_picNumber.pPic == NULL)
    {
        return -1;
    }
    setPicSrcRect(&g_picNumber, 0, NUMBER_SRC_Y, NUMBER_SRC_W, NUMBER_SRC_H);
    setPicDstRect(&g_picNumber, 0, 0, NUMBER_DST_W, NUMBER_DST_H);

    return 0;
}

//=========================================================
// 名称：Picture loadPic(const char* path))
// 说明：加载一幅图片
// 参数：path 本地图片路径
// 返回：图片结构体
//=========================================================
Picture loadPic(const char* path)
{
    Picture pic;

    pic.pPic = IMG_LoadTexture(g_pRenderer, path);
    if (NULL == pic.pPic)
    {
        printf("图片创建失败：%s", SDL_GetError());
    }
    return pic;
}

//=========================================================
// 名称：void setPicSrcRect(Picture * picture, int x, int y,
//              int w, int h)
//说明：设置一幅图片的源读取区域
//参数：picture 图片结构体指针
//      x, y 目标区域坐标
//      w, h 目标区域大小
//=========================================================
void setPicSrcRect(Picture* picture, int x, int y, int w, int h)
{
    picture->srcRect.x = x;
    picture->srcRect.y = y;

    if (x == 0 || y == 0)
    {
        SDL_QueryTexture(picture->pPic, NULL, NULL, &(picture->srcRect.w), &(picture->srcRect.h));
    }
    else
    {
        picture->srcRect.w = w;
        picture->srcRect.h = h;
    }
}

//=========================================================
//名称:void setPicDstRect(Picture * picture, int x, int y,
//              int w, int h)
//说明：设置一幅图片的目标显示区域
//参数: picture 图片结构体指针
//      x, y 目标区域坐标
//      w, h 目标区域大小
//=========================================================
void setPicDstRect(Picture* picture, int x, int y, int w, int h)
{
    picture->dstRect.x = x;
    picture->dstRect.y = y;
    picture->dstRect.w = w;
    picture->dstRect.h = h;
}

//=========================================================
// 名称 : void paintPic(Picture * picture)
// 说明：往渲染器绘制一幅图片
//参数：picture 图片结构体指针
//=========================================================
void paintPic(Picture* picture)
{
    SDL_RenderCopy(g_pRenderer, picture->pPic,
        &(picture->srcRect), &(picture->dstRect));
}

void freeSDL(void)
{
    /*释放渲染器*/
    SDL_DestroyRenderer(g_pRenderer);

    /*释放窗口*/
    SDL_DestroyWindow(g_pWindow);

    //释放音乐
    if(SHOW_WIN_MUSIC) Mix_FreeMusic(musicWin);
    if(SHOW_FAIL_MUSIC) Mix_FreeMusic(musicFail);

    //退出SDL_mixer
    Mix_CloseAudio();

    /*退出SDL库*/
    SDL_Quit();
}


int initSDL(void)
{
    //初始化SDL模块，参数：初始化所有子系统
    if (0 != SDL_Init(SDL_INIT_EVERYTHING))
    {
        printf("初始化失败：%s\n", SDL_GetError());
        return -1;
    }
    printf("初始化成功。\n");

    //初始化SDL_mixer
    if (Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) == -1)//频率，格式（非WAV要添加解码器），通道数量，样本大小
    {
        return -4;
    }
    printf("初始化音乐成功。\n");

    //获得最大可用窗口
    if (0 != SDL_GetDisplayUsableBounds(0, &g_game.g_MaxWindow))//默认获得主屏幕可用窗口大小
    {
        printf("获取窗口最大值失败：%s\n", SDL_GetError());
        return -5;
    }
    printf("获取可用窗口最大值成功\n");

    //获得最大格子数量
    g_game.g_MaxPlaidXNum = max((g_game.g_MaxWindow.w - 20 - 19) / 47,50);
    g_game.g_MaxPlaidYNum = max((g_game.g_MaxWindow.h - 30 - 21 - 80) / 47,50);

    return 0;
}


